package uutalk.netcloud.talk.model.bean;

import java.util.List;

import uutalk.netcloud.talk.app.BootstrapApp;
import uutalk.netcloud.talk.base.util.thirdpart.DBManager;

/**
 * Created by hujie on 17/4/10.
 */

public class DBEntityManager {

    public static UserInfo getUserInfoById(String userId) {
        UserInfoDao dao = (UserInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(UserInfo.class);
        return dao.load(userId);
    }

    public static void saveUserInfo(UserInfo userInfo) {
        UserInfoDao dao = (UserInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(UserInfo.class);
        dao.insertOrReplace(userInfo);
    }

    public static List<UserInfo> getAllUserInfo() {
        UserInfoDao dao = (UserInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(UserInfo.class);
        return dao.loadAll();
    }

    public static void saveGroupMember(GroupMember info) {
        GroupMemberDao dao = (GroupMemberDao) DBManager.getInstance(BootstrapApp.get()).getDao(GroupMember.class);
        dao.insertOrReplace(info);
    }

    public static GroupMember getGroupMember(String userID) {
        GroupMemberDao dao = (GroupMemberDao) DBManager.getInstance(BootstrapApp.get()).getDao(GroupMember.class);
        return dao.load(userID);
    }

    public static void updateGroupMemberStatus(String userID, String status){
        GroupMember groupMember = getGroupMember(userID);
        if(groupMember != null){
            groupMember.setUser_status(status);
            saveGroupMember(groupMember);
        }
    }

    public static void deleteGroupMember(GroupMember groupMember) {
        GroupMemberDao dao = (GroupMemberDao) DBManager.getInstance(BootstrapApp.get()).getDao(GroupMember.class);
        dao.delete(groupMember);
    }

    public static void deleteAllGroupMember() {
        GroupMemberDao dao = (GroupMemberDao) DBManager.getInstance(BootstrapApp.get()).getDao(GroupMember.class);
        dao.deleteAll();
    }


    public static List<GroupMember> getAllGroupMember(String groupId) {
        GroupMemberDao dao = (GroupMemberDao) DBManager.getInstance(BootstrapApp.get()).getDao(GroupMember.class);
        return dao.queryBuilder().where(GroupMemberDao.Properties.Group_id.eq(groupId)).build().list();
    }

    /**
     * 保存好友信息
     * @param friendInfoBean
     */
    public static void saveFriendInfoBean(FriendInfoBean friendInfoBean) {
        FriendInfoBeanDao dao = (FriendInfoBeanDao) DBManager.getInstance(BootstrapApp.get()).getDao(FriendInfoBean.class);
        dao.insertOrReplace(friendInfoBean);
    }

    /**
     * 删除好友信息
     * @param friend_id
     */
    public static void deleteFriendInfoBean(String friend_id) {
        FriendInfoBeanDao dao = (FriendInfoBeanDao) DBManager.getInstance(BootstrapApp.get()).getDao(FriendInfoBean.class);
        dao.deleteByKey(friend_id);
    }

    /**
     * 获取好友信息
     * @param friend_id
     * @return
     */
    public static FriendInfoBean getFriendInfoBean(String friend_id) {
        FriendInfoBeanDao dao = (FriendInfoBeanDao) DBManager.getInstance(BootstrapApp.get()).getDao(FriendInfoBean.class);
        return dao.load(friend_id);
    }

    /**
     * 保存文件记录
     * @param talkHistoryInfo
     */
    public static void saveTalkHistoryInfo(TalkHistoryInfo talkHistoryInfo) {
        TalkHistoryInfoDao dao = (TalkHistoryInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(TalkHistoryInfo.class);
        dao.insertOrReplace(talkHistoryInfo);
    }

    /**
     * 获取录音文件记录
     * @return
     */
    public static List<TalkHistoryInfo> getTalkHistoryInfos() {
        TalkHistoryInfoDao dao = (TalkHistoryInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(TalkHistoryInfo.class);
        return dao.loadAll();
    }


    public static TalkHistoryInfo getTalkHistoryInfo(String fileName) {
        TalkHistoryInfoDao dao = (TalkHistoryInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(TalkHistoryInfo.class);
        return dao.load(fileName);
    }

    /**
     * 更新本地数据库上传状态
     * @param voice_url
     */
    public static void updateTalkHistoryInfoBindServerByUrl(String voice_url) {
        TalkHistoryInfoDao dao = (TalkHistoryInfoDao) DBManager.getInstance(BootstrapApp.get()).getDao(TalkHistoryInfo.class);
        TalkHistoryInfo talkHistoryInfo = dao.queryBuilder().where(TalkHistoryInfoDao.Properties.Voice_url.eq(voice_url)).unique();
        if (talkHistoryInfo != null) {
            talkHistoryInfo.setIsBindServer(true);
            dao.insertOrReplace(talkHistoryInfo);
        }
    }
}
